var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '',
    memberinfo: {},
    phone: '',
    userSharedUrl: '',
    birthday: '2000-01-01',
    gender: '',
    realname: '',
    email: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getmemberinfo();
  },
  onShow: function () {
    let that = this;
    //如果无分享推广码,则需要获取分享二维码
    if (this.data.hasLogin && this.data.userSharedUrl == '') {
      that.getUserSharedUrl();
    }
  },
  getUserSharedUrl: function () {
    let that = this;
    util.request(api.GetSharedUrl).then(function (res) {
      that.setData({
        userSharedUrl: res.data.userSharedUrl
      });
    });
  },
  //获取电话号码
  getPhoneNumber: function (e) {
    let that = this;
    if (e.detail.errMsg !== "getPhoneNumber:ok") {
      // 拒绝授权
      return;
    }
    var iv = e.detail.iv;
    var encryptedData = e.detail.encryptedData;
    var appid = app.globalData.appid;
    wx.login({
      success(res) {
        var code = res.code
        util.request(api.GetSessionkey, {
          code: code,
          appid: appid
        }, 'POST').then(function (res) {
          if (res.result == 1) {
            util.request(api.AuthBindPhone, {
              iv: iv,
              encryptedData: encryptedData,
              sessionKey: res.data.session_key
            }).then(function (res) {
              if (res.result == 1) {
                that.setData({
                  phone: res.data.phone //设置手机号码
                });
              } else {
                wx.showToast({
                  title: '获取信息失败',
                  icon: 'none'
                })
              }
            }).catch((err) => {
              wx.showToast({
                title: '获取信息失败',
                icon: 'fail'
              })
            });;
          }
        });
      }
    })
  },
  //修改真实姓名
  realname: function (e) {
    let that = this;
    that.setData({
      realname: e.detail.value //设置手机号码
    });
  },
  //修改电子邮箱
  email: function (e) {
    let that = this;
    that.setData({
      email: e.detail.value //设置手机号码
    });
  },
  //修改性别
  radioChange: function (e) {
    let that = this;
    that.setData({
      gender: e.detail.value //设置手机号码
    });
  },

  // 保存推广码到相册
  saveShare: function () {
    let that = this;
    wx.downloadFile({
      url: that.data.userSharedUrl,
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            wx.showModal({
              title: '推广码下载',
              content: '推广二维码成功保存到相册!!',
              showCancel: false,
              confirmText: '好的',
              confirmColor: '#a78845',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                }
              }
            })
          },
          fail: function (res) {
            console.log('fail')
          }
        })
      },
      fail: function () {
        console.log('fail')
      }
    })
  },
  //修改出生日期
  bindDateChange: function (e) {
    this.setData({
      birthday: e.detail.value
    })
  },
  //保存
  baocun: function () {
    let that = this;
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '是否要保存？',
      success: function (res) {
        if (!res.confirm) {
          return;
        }
        util.request(api.Mywanshanmember, {
          mobile: that.data.phone,
          realname: that.data.realname,
          birthday: that.data.birthday,
          gender: that.data.gender,
          email: that.data.email
        }, 'POST').then(function (res) {
          if (res.result == 1) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              success: function () {
                wx.navigateBack({
                  delta: 1
                })
              }
            });
          }
        });
      }
    })
  },
  //取消
  quxiao: function () {
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '是否要取消？',
      success: function (res) {
        if (!res.confirm) {
          return;
        }
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  //获取会员信息
  getmemberinfo: function () {
    let that = this;
    util.request(api.MemberInfo).then(function (res) {
      if (res.result == 1) {
        that.setData({
          memberinfo: res.data,
          phone: res.data.mobile,
          gender: res.data.gender,
          realname: res.data.realname,
          email: res.data.email
        });
        if (res.data.birthday) {
          that.setData({
            birthday: res.data.birthday
          });
        }
      }
    });
  },
})